package com.proton.carepatchtemp.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.AddProfileActivity;
import com.proton.carepatchtemp.adapter.PrfChooseAdapter;
import com.proton.carepatchtemp.base.BaseFragment;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.daomanager.ProfileManager;
import com.proton.carepatchtemp.database.table.Device;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;

import java.util.ArrayList;
import java.util.List;

public class ChooseProfileFragment extends BaseFragment {
    Device tempDevice;
    List<Profile> mProfiles = new ArrayList<>();
    //  控件
    private RecyclerView mRcvProfileList;
    private TextView mTvAddProfile;
    private PrfChooseAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    public ChooseProfileFragment() {
    }

    public static ChooseProfileFragment newInstance() {
        return new ChooseProfileFragment();
    }

    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.NET_PRF_ADD:
                if (event.isSuccess()) {
                    mProfiles.add((Profile) event.getObj());
                    mAdapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new PrfChooseAdapter(getActivity(), mProfiles);
        mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_choose_profile, container, false);
        initView(view);
        setListener();
        return view;
    }

    private void setListener() {
        mTvAddProfile.setOnClickListener(view -> {
            Intent goAddPrf = new Intent(getActivity(), AddProfileActivity.class);
            getActivity().startActivity(goAddPrf);
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mProfiles.clear();
        mProfiles.addAll(ProfileManager.getAll());
        mRcvProfileList.setVisibility(View.VISIBLE);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
            return;
        }
        mProfiles.clear();
        mProfiles.addAll(ProfileManager.getAll());
        mRcvProfileList.setVisibility(View.VISIBLE);
        mAdapter.notifyDataSetChanged();
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    public Device getTempDevice() {
        return tempDevice;
    }

    public void setTempDevice(Device tempDevice) {
        this.tempDevice = tempDevice;
        LogUtils.e(tempDevice);
    }

    private void initView(View view) {
        mRcvProfileList = view.findViewById(R.id.rcv_profile_list);
        mTvAddProfile = view.findViewById(R.id.tv_add_profile);
        mRcvProfileList.setLayoutManager(mLinearLayoutManager);
        mRcvProfileList.setAdapter(mAdapter);
    }
}
